package com.shu.solr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

public class TestSolrQuery {

	public static void main(String[] args) throws Exception, IOException {
		final String solrUrl = "http://192.168.1.14:8984/solr/weighthistory_day";
		
		HttpSolrClient client = new HttpSolrClient.Builder(solrUrl)
			    .withConnectionTimeout(10000)
			    .withSocketTimeout(60000)
			    .build();


		SolrQuery query = new SolrQuery("*:*");  
		query.setRows(1000);
		query.setStart(1001);
		query.addSort("create_time", ORDER.asc);
        QueryResponse resp = client.query(query);  
        
        int size = resp.getResults().size();
        long total = resp.getResults().getNumFound();
        SolrDocumentList list = resp.getResults();
        List<SolrInputDocument> inpList = new ArrayList<SolrInputDocument>();
        for(int i=0; i<size; i++) {
        	SolrDocument doc = list.get(i);
        	Collection<String> ks = doc.getFieldNames();
        	SolrInputDocument input = new SolrInputDocument();
        	for (String k : ks) {
        		if(!"_version_".equals(k)){
        			input.addField(k, doc.getFieldValue(k));
        		}
        	}  
        	inpList.add(input);
        }

        final String zkHosts = "192.168.1.17:2181,192.168.1.18:2181,192.168.1.19:2181";
        SolrClient cloudClent = new CloudSolrClient.Builder().withZkHost(zkHosts).withZkChroot("/solrcloud").build();
        cloudClent.add("weighthistory_day",inpList);
        cloudClent.commit("weighthistory_day");
        cloudClent.close();
        
	}

}
